package com.example.rss_reder;


import java.util.ArrayList;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

@SuppressLint("HandlerLeak")
public class MainActivity extends Activity {
	MainAdapter adapter;
	ArrayList<WeatherList> data;
	ArrayList<PlaceList> plist;
	String place[][] = {{"성남시 분당구","구미동"},{"서울시 중구","을지로동"},{"경주시 강동면","오금리"},{"전주시 덕진구","호성동"},{"속초시","금호동"}};
	ListView lv;
//	String u[] = {"4113567000","1114060500","4713037000","4511362000","4221054000"};
	String x[] = {"62","60","101","63","87"};
	String y[] = {"122","127","93","90","141"};
	private final int DID = 0;
	private final int WHAT = 0;
	
	Handler handler = new Handler(){
		public void handleMessage(Message msg){
			switch (msg.what){
				case WHAT:
					lv = (ListView)findViewById(R.id.lv_all);
					lv.setAdapter(adapter);
					lv.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							// TODO Auto-generated method stub
							Intent i = new Intent(MainActivity.this, SubActivity.class);
							//x,y
							i.putExtra("url", "http://www.kma.go.kr/wid/queryDFS.jsp?gridx="+x[arg2]+"&gridy="+y[arg2]);
							//code
							//i.putExtra("url", "http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone="+y[arg2]);
							i.putExtra("place", plist.get(arg2).getPlace());
							
							startActivity(i);
							}
						
					});
					
					removeDialog(DID);
					break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		
		Display display = ((WindowManager)
				this.getSystemService(Service.WINDOW_SERVICE)).getDefaultDisplay();
			float windowWidth = display.getWidth();
		
			int w1 = (int)(windowWidth/10*3);
			int w2 = (int)(windowWidth/10*3.5);
			TextView tv1 = (TextView)findViewById(R.id.tv1);
			tv1.setWidth(w1);
			TextView tv2 = (TextView)findViewById(R.id.tv2);
			tv2.setWidth(w2);
			TextView tv3 = (TextView)findViewById(R.id.tv3);
			tv3.setWidth(w2);
			
		callList();

	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		//return super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0)
		  {
			   AlertDialog.Builder bld = new AlertDialog.Builder(this);
			   bld.setTitle("종료");
			   bld.setMessage("종료하시겠습니까?");
			   bld.setPositiveButton("예", new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int which) {
			     moveTaskToBack(true);
			     stopService(new Intent("org.cherry.namespace.mymp3"));
			     finish();
			    }
			   });
			   bld.setNegativeButton("아니오",null);
			   bld.show();
			   return true; 
			  }
			  return super.onKeyDown(keyCode, event);
			 }




	void callList(){
		showDialog(DID);
		plist = new ArrayList<PlaceList>();
		
		Thread thread = new Thread(){
			public void run(){

				for(int i=0;i<x.length;i++){
//				for(int i=0;i<u.length;i++){
				data = new ArrayList<WeatherList>();
				//code
//				MyParsing p = new MyParsing("http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone="+u[i]);
				//x,y
				MyParsing p = new MyParsing("http://www.kma.go.kr/wid/queryDFS.jsp?&gridx="+x[i]+"&gridy="+y[i]);
				data = p.getData();
				plist.add(new PlaceList(place[i], data));
				}
				
				adapter = new MainAdapter(MainActivity.this, plist); 
			
				Message msg = handler.obtainMessage(WHAT);
				handler.sendMessage(msg);

			};
		};
		
		thread.start();
	
	}
	
	protected Dialog onCreateDialog(int id){
		ProgressDialog d = new ProgressDialog(this);
		switch(id){
			case DID:
				d.setMessage("Loding...");
				d.setIndeterminate(true);
				d.setCancelable(true);
				return d;
		}
		return d;
	}
	
}
